﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using GMS.DataAccessDapper;
using GMS.IServices;
using GMS.Models.Dto;
using GMS.Models.Entity;
using GMS.Models.Other;
using GMS.Models.ResponseDto;
using GMS.Models.SearchDto;
using GMS.Utils;

namespace GMS.Services
{
    public class UserInfoService : IUserInfoService
    {
        private readonly DapperUtil _dapper;
        private readonly IMapper _mapper;

        public UserInfoService(DapperUtil dapper, IMapper mapper)
        {
            _dapper = dapper;
            _mapper = mapper;
        }

        public async Task<int> DeleteAsync(Guid id)
        {
            string strSql = "Delete from userInfo where Id=@id";
            return await _dapper.ExecuteAsync(strSql, new { id });
        }

        public async Task<int> DeleteManyAsync(string ids)
        {
            var userIds = ids.Split(",").ToArray();
            string strSql = "Delete from userInfo where Id in @ids";
            return await _dapper.ExecuteAsync(strSql, new { ids = userIds });
        }

        public async Task<int> UpdatedAsync(UserInfo userInfo)
        {
            string strSql = "update UserInfo set IsDel=@IsDel where Id=@Id";
            return await _dapper.ExecuteAsync(strSql, userInfo);
        }

        public async Task<UserInfo> LoginAsync(string userName, string passWord)
        {
            string strSql = "select * from userInfo where userName=@userName and passWord=@passWord;";
            return await _dapper.QuerySingleAsync<UserInfo>(strSql, new { userName, passWord });
        }

        public async Task<UserInfo> GetUserAsync(Guid id)
        {
            string strSql = "Select * from UserInfo where Id=@id";
            return await _dapper.QuerySingleAsync<UserInfo>(strSql, new { id });
        }

        public async Task<UserInfo> GetSingleAsync(UserInfo userInfo)
        {
            string strSql = "select * from userInfo where userName=@userName and passWord=@passWord;";
            return await _dapper.QuerySingleAsync<UserInfo>(strSql, userInfo);
        }

        public async Task<int> AddedAsync(UserInfo user)
        {
            user.Id = GuidHelper.NewSeqGuid();
            string strSql = @"INSERT INTO dbo.UserInfo(Id, UserName, PassWord)
                                        Values(@id,@userName,@passWord)";
            EntityHelper<UserInfo>.SetComm(user);
            return await _dapper.ExecuteAsync(strSql, user);
        }

        public async Task<PagedList<UserInfoResponseDto>> GetMultipleAsync(UserInfoSearchDto dto)
        {
            string strSql = "select * from userInfo where 1=1 and userName<>'supper' ";
            if (!string.IsNullOrEmpty(dto.UserName))
            {
                dto.UserName = $"%{dto.UserName}%";
                strSql += " and userName like @userName";
            }
            strSql += " and isDel=0";
            strSql += @" order by userName asc ";
            strSql += $"OFFSET {(dto.PageNumber - 1) * dto.PageSize } ROW FETCH NEXT {dto.PageSize} ROW ONLY";
            var entities = await _dapper.QueryMultipleAsync<UserInfo>(strSql, dto);
            var responseDtos = _mapper.Map<List<UserInfoResponseDto>>(entities);
            var total = await GetTotal(dto);
            var pagedList = await PagedList<UserInfoResponseDto>.CreateAsync(responseDtos, total, dto.PageNumber, dto.PageSize);
            return await Task.FromResult(pagedList);
        }

        public async Task<int> GetTotal(UserInfoSearchDto dto)
        {
            string strSql = "select count(1) from userInfo where 1=1 and userName<>'supper' ";
            if (!string.IsNullOrEmpty(dto.UserName))
            {
                dto.UserName = $"%{dto.UserName}%";
                strSql += " and userName like @userName";
            }
            strSql += " and isDel=0";

            var res = await _dapper.ExecuteScalarAsync(strSql, dto);
            return await Task.FromResult(res.ToInt32());

        }

    }
}